package com.daidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.daidai.dao.UserDao;
import com.daidai.model.User;
import com.daidai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 16:24
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findAllUsers() {
        return userDao.selectList(null);
    }

    @Override
    public User validUser(int userId, String password) {
        return userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserId, userId).eq(User::getPassword, password));
    }

    @Override
    public User validUserAccount(int userId) {
        return userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserId, userId));
    }

    @Override
    public User findUserById(int userId) {
        return userDao.selectById(userId);
    }

    @Override
    public int findUserTypeById(int userId) {
        return userDao.selectOne(new LambdaQueryWrapper<User>().select(User::getType).eq(User::getUserId, userId)).getType();
    }

    @Override
    public void addUser(int userId, String password, int type) {
        userDao.insert(new User(userId, password, type));
    }

    @Override
    public void deleteUser(int userId) {
        userDao.deleteById(userId);
    }

    @Override
    public void updateUserPassword(int userId, String password) {
        userDao.updateById(new User().setUserId(userId).setPassword(password));
    }

    @Override
    public void updDateUserType(int userId, int type) {
        userDao.updateById(new User().setUserId(userId).setType(type));
    }
}
